package ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import td.Field;
import td.FieldElement;

/*
 * View class to represent Fields on the map.
 * */
public class FieldView extends JPanel implements Viewable {

	/*Field to represent*/
	protected Field field;
	/*Point2D nonused. Here for animated upgrade.*/
	private Point2D coord;
	/*Image to store displayabe.*/
	private Image img;
	
	public FieldView(Field field){		
		this.field=field;
		this.setLayout(new GridBagLayout());
		update();
	}

	public void setCoord(Point2D coordinates){
		this.coord=coordinates;
	}
	
	public Point2D getCoord(){
		return this.coord;
	}
	
	/*
	 * Function to update the field's current state.
	 * Displays every Fieldelement's viewable on this panel
	 * and checks if this field is either a road or not.
	 * */
	public void update(){
		this.removeAll();
		GridBagConstraints gridc = new GridBagConstraints();
		gridc.gridx=0;
		gridc.gridy=0;
		ArrayList<FieldElement> elements = field.getElements();
		if (!elements.isEmpty()) {
			for (int i = 0; i < elements.size(); i++) {
				add((JPanel) elements.get(i).viewable, gridc);
			}
		}
		if(field.isRoad){
			img = Toolkit.getDefaultToolkit().getImage("Images/road.png");
			JLabel picLabel = new JLabel(new ImageIcon(img));
			add(picLabel,gridc);
		}
		else{
			img = Toolkit.getDefaultToolkit().getImage("Images/field.png");
			JLabel picLabel = new JLabel(new ImageIcon(img));
			add(picLabel,gridc);
		}
	}
	
	/*public void paintComponent(Graphics g){
		super.paintComponents(g);
		Graphics2D g2D = (Graphics2D) g;
		g2D.drawImage(img, 0, 0, this);
	}*/
}
